<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
  
<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity">

  <!-- EXECUTION INSERT -->

  <insert id="insertExecution" parameterType="org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity">
    insert into ${prefix}ACT_RU_EXECUTION
    (
      ID_,
      ROOT_PROC_INST_ID_,
      PROC_INST_ID_,
      BUSINESS_KEY_,
      PROC_DEF_ID_,
      ACT_ID_,
      ACT_INST_ID_,
      IS_ACTIVE_,
      IS_CONCURRENT_,
      IS_SCOPE_,
      IS_EVENT_SCOPE_,
      PARENT_ID_,
      SUPER_EXEC_,
      SUPER_CASE_EXEC_,
      CASE_INST_ID_,
      SUSPENSION_STATE_,
      CACHED_ENT_STATE_,
      SEQUENCE_COUNTER_,
      TENANT_ID_,
      REV_
    )
    values
    (
      #{id ,jdbcType=VARCHAR},
      #{rootProcessInstanceIdRaw, jdbcType=VARCHAR},
      #{processInstanceId, jdbcType=VARCHAR},
      #{businessKeyWithoutCascade, jdbcType=VARCHAR},
      #{processDefinitionId ,jdbcType=VARCHAR},
      #{activityId ,jdbcType=VARCHAR},
      #{activityInstanceId ,jdbcType=VARCHAR},
      #{isActive ,jdbcType=BOOLEAN},
      #{isConcurrent ,jdbcType=BOOLEAN},
      #{isScope ,jdbcType=BOOLEAN},
      #{isEventScope ,jdbcType=BOOLEAN},
      #{parentId, jdbcType=VARCHAR},
      #{superExecutionId, jdbcType=VARCHAR},
      #{superCaseExecutionId, jdbcType=VARCHAR},
      #{caseInstanceId, jdbcType=VARCHAR},
      #{suspensionState, jdbcType=INTEGER},
      #{cachedEntityState, jdbcType=INTEGER},
      #{sequenceCounter, jdbcType=BIGINT},
      #{tenantId, jdbcType=VARCHAR},
      1
    )
  </insert>

  <!-- EXECUTION UPDATE -->
  
  <update id="updateExecution" parameterType="org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity">
    update ${prefix}ACT_RU_EXECUTION set
      REV_ = #{revisionNext, jdbcType=INTEGER},
      PROC_DEF_ID_ = #{processDefinitionId, jdbcType=VARCHAR},
      BUSINESS_KEY_ = #{businessKeyWithoutCascade, jdbcType=VARCHAR},
      ACT_ID_ = #{activityId, jdbcType=VARCHAR},
      ACT_INST_ID_ = #{activityInstanceId, jdbcType=VARCHAR},
      IS_ACTIVE_ = #{isActive, jdbcType=BOOLEAN},
      IS_CONCURRENT_ = #{isConcurrent, jdbcType=BOOLEAN},
      IS_SCOPE_ = #{isScope, jdbcType=BOOLEAN},
      IS_EVENT_SCOPE_ = #{isEventScope, jdbcType=BOOLEAN},
      PARENT_ID_ = #{parentId, jdbcType=VARCHAR},
      SUPER_EXEC_ = #{superExecutionId, jdbcType=VARCHAR},
      SUSPENSION_STATE_ = #{suspensionState, jdbcType=INTEGER},
      CACHED_ENT_STATE_ = #{cachedEntityState, jdbcType=INTEGER},
      SEQUENCE_COUNTER_ = #{sequenceCounter, jdbcType=BIGINT},
      TENANT_ID_ = #{tenantId, jdbcType=BIGINT}
    where ID_ = #{id, jdbcType=VARCHAR}
      and REV_ = #{revision, jdbcType=INTEGER}
  </update>
  
  <update id="updateExecutionSuspensionStateByParameters" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    update ${prefix}ACT_RU_EXECUTION set
      REV_ = 1 + REV_ ,
      SUSPENSION_STATE_ = #{parameter.suspensionState, jdbcType=INTEGER}
    <where>
      <if test="parameter.processInstanceId != null">
        PROC_INST_ID_ = #{parameter.processInstanceId, jdbcType=VARCHAR}
      </if>
      <if test="parameter.processDefinitionId != null">
        and PROC_DEF_ID_ = #{parameter.processDefinitionId, jdbcType=VARCHAR}
      </if>
      <if test="parameter.processDefinitionKey != null">
        and PROC_DEF_ID_ IN (
          SELECT ID_ 
          FROM ${prefix}ACT_RE_PROCDEF PD
          WHERE PD.KEY_ = #{parameter.processDefinitionKey, jdbcType=VARCHAR}
          <if test="parameter.isTenantIdSet">
            <if test="parameter.tenantId != null">
              and PD.TENANT_ID_ = #{parameter.tenantId, jdbcType=VARCHAR}
            </if>
            <if test="parameter.tenantId == null">
              and PD.TENANT_ID_ is null
            </if>
          </if>
        )
        <bind name="columnPrefix" value="''"/>
        <include refid="org.camunda.bpm.engine.impl.persistence.entity.TenantEntity.queryTenantCheckWithPrefix" />
      </if>
    </where>
  </update>

  <!-- EXECUTION DELETE -->

  <delete id="deleteExecution" parameterType="org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity">
    delete from ${prefix}ACT_RU_EXECUTION where ID_ = #{id} and REV_ = #{revision}
  </delete>

  <!-- EXECUTION RESULTMAP -->

  <resultMap id="executionResultMap" type="org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="revision" column="REV_" jdbcType="INTEGER" />
    <result property="rootProcessInstanceId" column="ROOT_PROC_INST_ID_" jdbcType="VARCHAR"/>
    <result property="processInstanceId" column="PROC_INST_ID_" jdbcType="VARCHAR" />
    <result property="businessKey" column="BUSINESS_KEY_" jdbcType="VARCHAR" />
    <result property="processDefinitionId" column="PROC_DEF_ID_" jdbcType="VARCHAR" />
    <result property="activityId" column="ACT_ID_" jdbcType="VARCHAR" />
    <result property="activityInstanceId" column="ACT_INST_ID_" jdbcType="VARCHAR" />
    <result property="isActive" column="IS_ACTIVE_" jdbcType="BOOLEAN" />
    <result property="isConcurrent" column="IS_CONCURRENT_" jdbcType="BOOLEAN" />
    <result property="isScope" column="IS_SCOPE_" jdbcType="BOOLEAN" />
    <result property="isEventScope" column="IS_EVENT_SCOPE_" jdbcType="BOOLEAN" />
    <result property="parentId" column="PARENT_ID_" jdbcType="VARCHAR" />
    <result property="superExecutionId" column="SUPER_EXEC_" jdbcType="VARCHAR" />
    <result property="superCaseExecutionId" column="SUPER_CASE_EXEC_" jdbcType="VARCHAR" />
    <result property="caseInstanceId" column="CASE_INST_ID_" jdbcType="VARCHAR" />
    <result property="suspensionState" column="SUSPENSION_STATE_" jdbcType="INTEGER"/>
    <result property="cachedEntityState" column="CACHED_ENT_STATE_" jdbcType="INTEGER"/>
    <result property="sequenceCounter" column="SEQUENCE_COUNTER_" jdbcType="BIGINT"/>
    <result property="tenantId" column="TENANT_ID_" jdbcType="VARCHAR"/>
  </resultMap>
  
  <resultMap type="org.camunda.bpm.engine.impl.util.ImmutablePair" id="deploymentIdMapping">
    <id property="left" column="DEPLOYMENT_ID_" jdbcType="VARCHAR" />
    <id property="right" column="ID_" jdbcType="VARCHAR" />
  </resultMap>
  
  <!-- EXECUTION SELECT -->
  
  <select id="selectExecution" parameterType="string" resultMap="executionResultMap">
    select * from ${prefix}ACT_RU_EXECUTION where ID_ = #{id}
  </select>
  
  <select id="selectExecutionsByParentExecutionId" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultMap="executionResultMap">
    select * from ${prefix}ACT_RU_EXECUTION
    where PARENT_ID_ = #{parameter}
  </select>
  
  <select id="selectExecutionsByProcessInstanceId" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultMap="executionResultMap">
    select * from ${prefix}ACT_RU_EXECUTION
    where PROC_INST_ID_ = #{parameter}
  </select>

  <select id="selectProcessInstanceIdsByProcessDefinitionId" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultType="string">
    select ID_
    from ${prefix}ACT_RU_EXECUTION
    where PROC_DEF_ID_ = #{parameter} and PARENT_ID_ is null
  </select>
  
  <select id="selectExecutionsByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.ExecutionQueryImpl" resultMap="executionResultMap">
  	<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    ${limitBefore}
    select ${distinct} RES.*
    ${limitBetween}
    <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.orderBySelection"/>
    <include refid="selectExecutionsByQueryCriteriaSql"/>
    ${orderBy}
    ${limitAfter}
  </select>
  
  <select id="selectExecutionCountByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.ExecutionQueryImpl" resultType="long">
    ${countDistinctBeforeStart} RES.ID_ ${countDistinctBeforeEnd}
    <include refid="selectExecutionsByQueryCriteriaSql"/>
    ${countDistinctAfterEnd}
  </select>
  
  <select id="selectProcessInstanceByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.ProcessInstanceQueryImpl" resultMap="executionResultMap">
  	<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    ${limitBefore}
    select ${distinct} RES.*
    ${limitBetween}
    <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.orderBySelection"/>
    <include refid="selectProcessInstanceByQueryCriteriaSql"/>
    ${orderBy}
    ${limitAfter}
  </select>
  
  <select id="selectProcessInstanceCountByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.ProcessInstanceQueryImpl" resultType="long">
    ${countDistinctBeforeStart} RES.ID_ ${countDistinctBeforeEnd}
    <include refid="selectProcessInstanceByQueryCriteriaSql"/>
    ${countDistinctAfterEnd}
  </select>

  <select id="selectProcessInstanceIdsByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.ProcessInstanceQueryImpl" resultType="string">
    select distinct RES.ID_
    <include refid="selectProcessInstanceByQueryCriteriaSql"/>
  </select>
  
  <select id="selectProcessInstanceDeploymentIdMappingsByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.ProcessInstanceQueryImpl" resultMap="deploymentIdMapping">
    select distinct P.DEPLOYMENT_ID_, RES.ID_
    <include refid="selectProcessInstanceByQueryCriteriaSql"/>
    ORDER BY P.DEPLOYMENT_ID_ ASC
  </select>

  <sql id="selectProcessInstanceByQueryCriteriaSql">

    from ${prefix}ACT_RU_EXECUTION RES
    <bind name="INC_JOIN" value="false" />
    <bind name="EXE_JOIN" value="false" />
    <bind name="JOIN_TYPE" value="'inner join'" />

    <foreach collection="queries" item="query">
      <if test="query.isOrQueryActive">
        <bind name="JOIN_TYPE" value="'left join'" />
      </if>
      <if test="query != null &amp;&amp; (query.withIncident || query.incidentType != null || query.incidentId != null || query.incidentMessage != null || query.incidentMessageLike != null)">
        <bind name="INC_JOIN" value="true" />
      </if>
      <if test="query != null &amp;&amp; (query.activityIds != null &amp;&amp; query.activityIds.length > 0)">
        <bind name="EXE_JOIN" value="true" />
      </if>
    </foreach>

    <if test="INC_JOIN">
      ${JOIN_TYPE} ${prefix}ACT_RU_INCIDENT INC on RES.ID_ = INC.PROC_INST_ID_
    </if>
    <if test="EXE_JOIN">
      ${JOIN_TYPE} ${prefix}ACT_RU_EXECUTION EXE on RES.ID_ = EXE.PROC_INST_ID_
    </if>

    inner join ${prefix}ACT_RE_PROCDEF P on RES.PROC_DEF_ID_ = P.ID_

    <if test="authCheck.shouldPerformAuthorizatioCheck &amp;&amp; !authCheck.revokeAuthorizationCheckEnabled &amp;&amp; authCheck.authUserId != null">
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.authCheckJoinWithoutOnClause" /> 
      AUTH ON (AUTH.RESOURCE_ID_ ${authJoinStart} RES.ID_ ${authJoinSeparator} P.KEY_ ${authJoinSeparator} '*' ${authJoinEnd})
    </if>

    <where>
      <foreach collection="queries" item="query" index="i">
        <choose>
          <when test="i == 0">
            <bind name="queryType" value="'and'" />
          </when>
          <otherwise>
            <bind name="queryType" value="'or'" />
          </otherwise>
        </choose>
        and (
        <trim suffixOverrides="and">
          RES.PARENT_ID_ is null and
          <trim prefix="(" prefixOverrides="or|and" suffix=")">
            <if test="query.processDefinitionId != null">
              ${queryType} P.ID_ = #{query.processDefinitionId}
            </if>
            <if test="query.processDefinitionKey != null">
              ${queryType} P.KEY_ = #{query.processDefinitionKey}
            </if>
            <if test="query.processDefinitionKeys != null &amp;&amp; query.processDefinitionKeys.length > 0">
              ${queryType} P.KEY_ in
              <foreach item="item" index="index" collection="query.processDefinitionKeys"
                       open="(" separator="," close=")">
                #{item}
              </foreach>
            </if>
            <if test="query.processDefinitionKeyNotIn != null &amp;&amp; query.processDefinitionKeyNotIn.length > 0">
              ${queryType} P.KEY_ not in
              <foreach item="item" index="index" collection="query.processDefinitionKeyNotIn"
                       open="(" separator="," close=")">
                #{item}
              </foreach>
            </if>
            <if test="query.deploymentId != null">
              ${queryType} P.DEPLOYMENT_ID_ = #{query.deploymentId}
            </if>
            <if test="query.processInstanceId != null">
              ${queryType} RES.PROC_INST_ID_ = #{query.processInstanceId}
            </if>
            <if test="query.processInstanceIds != null and !query.processInstanceIds.isEmpty()">
              ${queryType}
              <bind name="listOfIds" value="query.processInstanceIds" />
              <bind name="fieldName" value="'RES.PROC_INST_ID_'" />
              <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.applyInForPaginatedCollection" />
            </if>
            <if test="query.businessKey != null">
              ${queryType} RES.BUSINESS_KEY_ = #{query.businessKey}
            </if>
            <if test="query.businessKeyLike != null">
              ${queryType} RES.BUSINESS_KEY_ like #{query.businessKeyLike} ESCAPE ${escapeChar}
            </if>
            <if test="query.superProcessInstanceId != null">
              <!-- A sub process instance is stored under a certain *execution*, potentially nested.
              A sub process instance is NOT stored under the process instance, hence the following: -->
              ${queryType} RES.SUPER_EXEC_ IN (select ID_ from ${prefix}ACT_RU_EXECUTION where PROC_INST_ID_ = #{query.superProcessInstanceId})
            </if>
            <if test="query.isRootProcessInstances == true">
              ${queryType} RES.SUPER_EXEC_ is null
            </if>
            <if test="query.isLeafProcessInstances">
              and RES.ID_ not in (
              select SUPER.PROC_INST_ID_ from ${prefix}ACT_RU_EXECUTION SUPER
              inner join ${prefix}ACT_RU_EXECUTION CALLED on SUPER.ID_ = CALLED.SUPER_EXEC_ where CALLED.ID_ = CALLED.PROC_INST_ID_)
            </if>
            <if test="query.subProcessInstanceId != null">
              ${queryType} RES.ID_ = (select PROC_INST_ID_ from ${prefix}ACT_RU_EXECUTION where ID_ = (select SUPER_EXEC_ from ${prefix}ACT_RU_EXECUTION where ID_ = #{query.subProcessInstanceId}))
            </if>
            <if test="query.suspensionState != null">
              ${queryType} RES.SUSPENSION_STATE_ = #{query.suspensionState.stateCode}
            </if>
            <if test="query.caseInstanceId != null">
              ${queryType} RES.CASE_INST_ID_ = #{query.caseInstanceId}
            </if>
            <if test="query.superCaseInstanceId != null">
              ${queryType} RES.SUPER_CASE_EXEC_ IN (select ID_ from ${prefix}ACT_RU_CASE_EXECUTION where CASE_INST_ID_ = #{query.superCaseInstanceId})
            </if>
            <if test="query.subCaseInstanceId != null">
              ${queryType} RES.ID_ = (select PROC_INST_ID_ from ${prefix}ACT_RU_EXECUTION where ID_ = (select SUPER_EXEC_ from ${prefix}ACT_RU_CASE_EXECUTION where ID_ = #{query.subCaseInstanceId}))
            </if>
            <!-- PLEASE NOTE: If you change anything have a look into the HistoricVariableInstance & HistoricProcessInstance, the same query object is used there! -->
            <foreach collection="query.queryVariableValues" index="index" item="queryVariableValue">
              ${queryType} EXISTS (
              select
              ID_
              from
              ${prefix}ACT_RU_VARIABLE
              WHERE
              <bind name="varPrefix" value="''"/>

              <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.variableNameEqualsCaseInsensitive" />

              <choose>
                <when test="queryVariableValue.local">
                  and RES.ID_ = EXECUTION_ID_
                </when>
                <otherwise>
                  <!-- When process instance or case instance variable is queried for, taskId should be null -->
                  and TASK_ID_ is null and RES.PROC_INST_ID_ = PROC_INST_ID_
                </otherwise>
              </choose>

              <bind name="varTypeField" value="'TYPE_'"/>
              <if test="queryVariableValue.valueConditions != null">
                and
                <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.variableValueConditions"/>
              </if>
              )
            </foreach>
            <if test="query.incidentType != null">
              ${queryType} INC.INCIDENT_TYPE_ = #{query.incidentType}
            </if>
            <if test="query.incidentId != null">
              ${queryType} INC.ID_ = #{query.incidentId}
            </if>
            <if test="query.incidentMessage != null">
              ${queryType} INC.INCIDENT_MSG_ = #{query.incidentMessage}
            </if>
            <if test="query.incidentMessageLike != null">
              ${queryType} INC.INCIDENT_MSG_ like #{query.incidentMessageLike} ESCAPE ${escapeChar}
            </if>
            <if test="query.isProcessDefinitionWithoutTenantId">
              ${queryType} P.TENANT_ID_ IS NULL
            </if>
            <if test="query.isTenantIdSet">
              <if test="query.tenantIds != null &amp;&amp; query.tenantIds.length > 0">
                ${queryType} RES.TENANT_ID_ in
                <foreach item="tenantId" index="index" collection="query.tenantIds"
                                 open="(" separator="," close=")">
                  #{tenantId}
                </foreach>
              </if>
              <if test="query.tenantIds == null">
                ${queryType} RES.TENANT_ID_ is null
              </if>
            </if>
            <if test="query.activityIds != null &amp;&amp; query.activityIds.length > 0">
              ${queryType} (EXE.IS_EVENT_SCOPE_ = ${falseConstant}
              and EXE.ACT_ID_ in
              <foreach item="activityId" index="index" collection="query.activityIds"
                             open="(" separator="," close=")">
                #{activityId}
              </foreach>
              )
            </if>
          </trim>
        </trim>
        )
      </foreach>

      <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.queryAuthorizationCheck" />
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.TenantEntity.queryTenantCheck" />

    </where>
  </sql>

  <sql id="selectExecutionsByQueryCriteriaSql">

    from ${prefix}ACT_RU_EXECUTION RES
    <if test="businessKey != null">
      inner join ${prefix}ACT_RU_EXECUTION INST on RES.PROC_INST_ID_ = INST.ID_
    </if>
    <if test="incidentType != null || incidentId != null || incidentMessage != null || incidentMessageLike != null">
      inner join ${prefix}ACT_RU_INCIDENT INC on RES.ID_ = INC.EXECUTION_ID_
    </if>
    inner join ${prefix}ACT_RE_PROCDEF P on RES.PROC_DEF_ID_ = P.ID_

    <if test="authCheck.shouldPerformAuthorizatioCheck &amp;&amp; !authCheck.revokeAuthorizationCheckEnabled &amp;&amp; authCheck.authUserId != null">
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.authCheckJoinWithoutOnClause" />
      AUTH ON (AUTH.RESOURCE_ID_ ${authJoinStart} RES.PROC_INST_ID_ ${authJoinSeparator} P.KEY_ ${authJoinSeparator} '*' ${authJoinEnd})
    </if>

    <where>
      <if test="processDefinitionId != null">
        and P.ID_ = #{processDefinitionId}
      </if>
      <if test="processDefinitionKey != null">
        and P.KEY_ = #{processDefinitionKey}
      </if>
      <if test="executionId != null">
        and RES.ID_ = #{executionId}
      </if>
      <if test="processInstanceId != null">
        and RES.PROC_INST_ID_ = #{processInstanceId}
      </if>
      <if test="businessKey != null">
        and INST.BUSINESS_KEY_ = #{businessKey}
      </if>
      <if test="activityId != null">
        and RES.ACT_ID_ = #{activityId}
      </if>
      <if test="suspensionState != null">
        and RES.SUSPENSION_STATE_ = #{suspensionState.stateCode}
      </if>
      <!-- PLEASE NOTE: If you change anything have a look into the HistoricVariableInstance & HistoricProcessInstance, the same query object is used there! -->
      <foreach collection="queryVariableValues" index="index" item="queryVariableValue">
        and EXISTS (
        select
        ID_
        from
        ${prefix}ACT_RU_VARIABLE
        WHERE
        <bind name="varPrefix" value="''"/>

        <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.variableNameEqualsCaseInsensitive" />

        <choose>
          <when test="queryVariableValue.local">
            and RES.ID_ = EXECUTION_ID_
          </when>
          <otherwise>
            <!-- When process instance or case instance variable is queried for, taskId should be null -->
            and TASK_ID_ is null and RES.PROC_INST_ID_ = PROC_INST_ID_
          </otherwise>
        </choose>

        <bind name="varTypeField" value="'TYPE_'"/>
        <if test="queryVariableValue.valueConditions != null">
          and
          <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.variableValueConditions"/>
        </if>
        )
      </foreach>
      <!-- event subscriptions -->
      <if test="eventSubscriptions != null">
        <foreach collection="eventSubscriptions" index="index" item="eventSubscriptionValue">
          and exists (select ID_ from ${prefix}ACT_RU_EVENT_SUBSCR EVT where
          EVT.EXECUTION_ID_ = RES.ID_ and
          EVT.EVENT_TYPE_ = #{eventSubscriptionValue.eventType}
          <if test = "eventSubscriptionValue.eventName != null">
            and EVT.EVENT_NAME_ = #{eventSubscriptionValue.eventName}
          </if>
          )
        </foreach>
      </if>
      <if test="incidentType != null">
        and INC.INCIDENT_TYPE_ = #{incidentType}
      </if>
      <if test="incidentId != null">
        and INC.ID_ = #{incidentId}
      </if>
      <if test="incidentMessage != null">
        and INC.INCIDENT_MSG_ = #{incidentMessage}
      </if>
      <if test="incidentMessageLike != null">
        and INC.INCIDENT_MSG_ like #{incidentMessageLike} ESCAPE ${escapeChar}
      </if>
      <if test="isTenantIdSet">
        <if test="tenantIds != null &amp;&amp; tenantIds.length > 0">
          and RES.TENANT_ID_ in
          <foreach item="tenantId" index="index" collection="tenantIds"
                   open="(" separator="," close=")">
            #{tenantId}
          </foreach>
        </if>
        <if test="tenantIds == null">
          and RES.TENANT_ID_ is null
        </if>
      </if>

      <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.queryAuthorizationCheck" />
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.TenantEntity.queryTenantCheck" />

    </where>
  </sql>

  <select id="selectSubProcessInstanceBySuperExecutionId" parameterType="string" resultMap="executionResultMap">
    select *
    from ${prefix}ACT_RU_EXECUTION
    where SUPER_EXEC_ = #{parameter}
  </select>

  <select id="selectSubProcessInstanceBySuperCaseExecutionId" parameterType="string" resultMap="executionResultMap">
    select *
    from ${prefix}ACT_RU_EXECUTION
    where SUPER_CASE_EXEC_ = #{parameter}
  </select>

  <select id="selectExecutionByNativeQuery" parameterType="java.util.Map" resultMap="executionResultMap">
    <if test="resultType == 'LIST_PAGE'">
      ${limitBefore}
    </if>
    ${sql}
    <if test="resultType == 'LIST_PAGE'">
      ${limitAfter}
    </if>
  </select>

  <select id="selectExecutionByNativeQuery_mssql_or_db2" parameterType="java.util.Map" resultMap="executionResultMap">
    <if test="resultType == 'LIST_PAGE'">
        ${limitBeforeNativeQuery}
    </if>
    ${sql} 
    <if test="resultType == 'LIST_PAGE'">
      ${limitAfter}
    </if>
  </select>

  <select id="selectExecutionCountByNativeQuery" parameterType="java.util.Map" resultType="long">
    ${sql}
  </select>  
  
</mapper>
